package cn.zjh.basic.controller;

import cn.zjh.basic.dto.SmsDto;
import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.service.IVerifyCodeService;
import cn.zjh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;

    /**
     * 获取图形验证码
     * @param Key
     * @return
     */
    @GetMapping("/image/{Key}")
    public AjaxResult imageCode(@PathVariable("Key") String Key){
        try {
            //
            String base64 = iVerifyCodeService.getImageCode(Key);
            return AjaxResult.me().setResultObj(base64);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败！！！");
        }
    }
    /**
     * 获取手机验证码
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsDto smsDto){
        try {
            //
            iVerifyCodeService.sentSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取手机验证码失败！！！");
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙！！！");
        }
    }


    /**
     * 获取手机验证码
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            iVerifyCodeService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙!!!");
        }
    }
}
